package com.ciaojian.store.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.enums.business.MaterialApplyRecordStatusEnum;
import com.ciaojian.core.model.MaterialApplyRecord;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.service.MaterialApplyRecordServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Atlakyn
 */
@Api(tags = "物资管理")
@RestController
@RequestMapping("/material")
public class MaterialController {
    @Resource
    private MaterialApplyRecordServiceImpl materialApplyRecordService;

    @ApiOperation("展示申请记录")
    @GetMapping("/list-apply")
    public Result listApply(
            MaterialApplyRecordStatusEnum status,
            Integer franchiseesId,
            @RequestParam(defaultValue = "0") Integer pageNo,
            @RequestParam(defaultValue = "6") Integer pageSize) {
        return Result.ok(materialApplyRecordService.getBaseMapper().findAllApplyRecord(franchiseesId, status, new Page<>(pageNo, pageSize)));
    }

    @ApiOperation("审核")
    @PostMapping("/apply-audit/{applyId}")
    public Result applyAudit(@PathVariable Integer applyId, MaterialApplyRecordStatusEnum status) {
        MaterialApplyRecord materialApplyRecord = new MaterialApplyRecord().selectById(applyId);
        Assert.notNull(materialApplyRecord, "申请记录不存在");
        return Result.save(materialApplyRecordService.applyAudit(materialApplyRecord, status));
    }

}
